package com.ruoyi.technicianAudit.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.check.CheckEmptyUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.technician.domain.Technician;
import com.ruoyi.technician.mapper.TechnicianMapper;
import com.ruoyi.technicianAudit.domain.TechnicianLifePhotoAudit;
import com.ruoyi.technicianAudit.service.ITechnicianLifePhotoAuditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 生活照审核Controller
 *
 * @author ruoyi
 * @date 2025-04-07
 */
@RestController
@RequestMapping("/technicianLifePhotoAudit/technicianLifePhotoAudit")
public class TechnicianLifePhotoAuditController extends BaseController {
    @Autowired
    private ITechnicianLifePhotoAuditService technicianLifePhotoAuditService;
    @Resource
    private TechnicianMapper technicianMapper;

    /**
     * 查询生活照审核列表
     */
    @PreAuthorize("@ss.hasPermi('technicianLifePhotoAudit:technicianLifePhotoAudit:list')")
    @GetMapping("/list")
    public TableDataInfo list(TechnicianLifePhotoAudit technicianLifePhotoAudit) {
        startPage();
        List<TechnicianLifePhotoAudit> list = technicianLifePhotoAuditService.selectTechnicianLifePhotoAuditList(technicianLifePhotoAudit);
        if (!CheckEmptyUtil.isEmpty(list)) {
            for (TechnicianLifePhotoAudit lifePhotoAudit : list) {
                String tName = technicianMapper.selectTName(lifePhotoAudit.getTid());
                lifePhotoAudit.setTname(CheckEmptyUtil.isEmpty(tName) ? "" : tName);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出生活照审核列表
     */
    @PreAuthorize("@ss.hasPermi('technicianLifePhotoAudit:technicianLifePhotoAudit:export')")
    @Log(title = "生活照审核", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TechnicianLifePhotoAudit technicianLifePhotoAudit) {
        List<TechnicianLifePhotoAudit> list = technicianLifePhotoAuditService.selectTechnicianLifePhotoAuditList(technicianLifePhotoAudit);
        if (!CheckEmptyUtil.isEmpty(list)) {
            for (TechnicianLifePhotoAudit lifePhotoAudit : list) {
                String tName = technicianMapper.selectTName(lifePhotoAudit.getTid());
                lifePhotoAudit.setTname(CheckEmptyUtil.isEmpty(tName) ? "" : tName);
            }
        }
        ExcelUtil<TechnicianLifePhotoAudit> util = new ExcelUtil<TechnicianLifePhotoAudit>(TechnicianLifePhotoAudit.class);
        util.exportExcel(response, list, "生活照审核数据");
    }

    /**
     * 获取生活照审核详细信息
     */
    @PreAuthorize("@ss.hasPermi('technicianLifePhotoAudit:technicianLifePhotoAudit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(technicianLifePhotoAuditService.selectTechnicianLifePhotoAuditById(id));
    }

    /**
     * 新增生活照审核
     */
    @PreAuthorize("@ss.hasPermi('technicianLifePhotoAudit:technicianLifePhotoAudit:add')")
    @Log(title = "生活照审核", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TechnicianLifePhotoAudit technicianLifePhotoAudit) {
        return toAjax(technicianLifePhotoAuditService.insertTechnicianLifePhotoAudit(technicianLifePhotoAudit));
    }

    /**
     * 修改生活照审核
     */
    @PreAuthorize("@ss.hasPermi('technicianLifePhotoAudit:technicianLifePhotoAudit:edit')")
    @Log(title = "生活照审核", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TechnicianLifePhotoAudit technicianLifePhotoAudit) {
        if (technicianLifePhotoAudit.getAuditStatus() == 1) {
            Technician technician = technicianMapper.selectTechnicianById(technicianLifePhotoAudit.getTid());
            technician.setLifePhoto(technicianLifePhotoAudit.getNewLifePhoto());
            technicianMapper.updateTechnician(technician);
        }
        return toAjax(technicianLifePhotoAuditService.updateTechnicianLifePhotoAudit(technicianLifePhotoAudit));
    }

    /**
     * 删除生活照审核
     */
    @PreAuthorize("@ss.hasPermi('technicianLifePhotoAudit:technicianLifePhotoAudit:remove')")
    @Log(title = "生活照审核", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(technicianLifePhotoAuditService.deleteTechnicianLifePhotoAuditByIds(ids));
    }
}
